import { login } from '../..//services/user';
import { ALL, TREND, NEW, PRICE } from '../../asserts/CommodityType';
import {
  getBannerDetail, getBannerList, getNavigationDetail,
  getNavigationList, getProductByNew, getProductBySales,
  getTabDetail, getTabList
} from '../../services/home';
Page({
  data: {
   
    navigationlist: [],
    tablist: [],
    background: ['blue', 'red', 'yellow'],
    tabIndex: 1,
    currentCommodities: [],
    activeTabId: ALL,
    navgoodslist: []
  },
  onShow() {
    my.setNavigationBar({
      title: '',
      backgroundColor: '#4BA3F9'
    })
    // my.getAuthCode({
    //   scopes: 'auth_base',
    //   success: (res) => {
    //     login({ 'code': res.authCode }).then((res) => {
    //     })
    //   },
    // });
  },
  tabClick(event) {
    this.setData({ tabIndex: event.target.dataset.num });
    this.getNavigationDetail(event.target.dataset.id)
  },
  onLoad(query) {
    // 页面加载
    this.getBannerList()//获取轮播图列表
    this.getNavigationList()//获取导航栏列表
    this.getTabList()//获取标签列表
  },
  //点击图片触发事件
  swipclick: function (e) {
    var id = e.target.dataset.id;
    var bannerImage = e.target.dataset.img;
    let data = {}
    data.bannerImage = encodeURIComponent(bannerImage);
    getBannerDetail({ bannerId: id }).then((res) => {
      data.goodsList = res.data
      this.toshopUI(data)
    })
  },

  toshopUI(data) {

    my.navigateTo({
      url: '/pages/shopUl/shopUl?data=' + JSON.stringify(data),
    })
  },
  getBannerList() {
    getBannerList({}).then((res) => {
      this.setData({ background: res.data });
    })
  },
  getNavigationList() {
    getNavigationList({}).then((res) => {
      if (res.code == "000000") {
        if (res.data.length > 0) {
          this.getNavigationDetail(res.data[0].id);
        }
      }
      this.setData({ navigationlist: res.data });
    })
  },
  getTabList() {
    getTabList({}).then((res) => {
      if (res.code == "000000") {

        this.setData({ tablist: res.data });
      }
    })
  },
  getTabDetail(e) {
    var id = e.target.dataset.id;
    var bannerImage = e.target.dataset.img;
    let data = {}
    data.bannerImage = encodeURIComponent(bannerImage);
    getTabDetail({ tabId: id }).then((res) => {
      if (res.code == "000000") {
        data.goodsList = res.data
        this.toshopUI(data)
      }
    })
  },
  getProductBySales() {
    getProductBySales({}).then((res) => {
      if (res.code == "000000") {
        let data = {}
        data.goodsList = res.data
        this.toshopUI(data)
      }
    })
  },
  getProductByNew() {
    getProductByNew({}).then((res) => {
      if (res.code == "000000") {
        let data = {}
        data.goodsList = res.data
        this.toshopUI(data)
      }
    })
  },

  getNavigationDetail(id) {
    getNavigationDetail({ navigationId: id }).then((res) => {
      if (res.code == "000000") {
        this.setData({ navgoodslist: res.data });
      }
    })
  },
  searchgoods(e) {
    my.navigateTo({
      url: '/pages/shopList/shopList?productname=' + e.detail.value
    })
  },


});
